# 创建model
import os

from langchain_core.tools import tool
# 引入Tavily Search
from langchain_tavily import TavilySearch

from langchain_openai import ChatOpenAI

base_url = os.environ.get("BAOCLOUD_DS_BASE_URL")
model_name = os.environ.get("BAOCLOUD_DS_MODEL_NAME")
os.environ["OPENAI_API_KEY"] = os.getenv("BAOCLOUD_DS_API_KEY")
os.environ["TAVILY_API_KEY"] = "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"

model = ChatOpenAI(base_url=base_url, model_name=model_name)

# res = model.invoke("你是谁？")
# print(res.content)

# max_results：最多返回3个结果
tavily_search = TavilySearch(max_results=2)

# res = tavily_search.invoke("上海的天气怎么样？")
# print(res)

# 将工具与model绑定
model_with_tools = model.bind_tools([tavily_search])

res = model_with_tools.invoke("今天是几号啊？")
print(res.content)

res = model_with_tools.invoke("中国的首都是哪个城市？")
print(f'model_result_content: {res.content}')
print(f'tools_result_content: {res.tool_calls}')

res1 = model_with_tools.invoke("上海的天气怎么样？")
print(f'model_result_content: {res1.content}')
print(f'tools_result_content: {res1.tool_calls}')
